package com.example.demo.netty;

import io.socket.client.IO;
import io.socket.client.Socket;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @Author: xiatian
 * @CreateTime: 2022/1/6 10:22 上午
 * @Description:
 */
public class MonitorServerAvailable {
    public static void main(String[] args) {
//        TimerTask timerTask = new TimerTask() {
//            @Override
//            public void run() {
//                System.out.println("Run timeTask:"+new Date());
//
//            }
//        };
//        Timer timer = new Timer();
//        timer.schedule(timerTask,5000,5000);
        connectServer();
    }

    public static void connectServer() {
        String url = "http://localhost:8888/hello";
        try {
            IO.Options options = new IO.Options();
            options.transports = new String[]{"websocket"};
            options.reconnectionAttempts = 1000000000;
            options.reconnectionDelay = 60000;// 失败重连的时间间隔
            options.timeout = 20000;// 连接超时时间(ms)
            // par1 是任意参数
            final Socket socket = IO.socket(url, options);
            socket.on(Socket.EVENT_CONNECT, objects -> {
                System.out.println("client: 连接成功");
                System.out.println("8888的识别通道可用");
                //做你想做的操作
            });
            socket.on(Socket.EVENT_CONNECTING, objects -> System.out.println("client: " + "连接中"));
            socket.on(Socket.EVENT_CONNECT_TIMEOUT, objects -> System.out.println("client: " + "连接超时"));
            socket.on(Socket.EVENT_CONNECT_ERROR, objects -> System.out.println("client: " + "连接失败"));
            socket.connect();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
